package cn.smbms.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.code.kaptcha.Producer;

import cn.smbms.pojo.User;
import cn.smbms.service.RoleService;
import cn.smbms.service.UserService;
import cn.smbms.util.Constants;
import cn.smbms.vo.ResultVo;

@Controller
public class LoginController {
	@Autowired
	UserService userService;
	@Autowired
	RoleService roleService;

	@Autowired
	private Producer captchaProducer = null;

	@RequestMapping(value = "verifyImage")
	public void getVerifyImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		HttpSession session = request.getSession();
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		response.addHeader("Cache-Control", "post-check=0, pre-check=0");
		response.setHeader("Pragma", "no-cache");
		response.setContentType("image/jpeg");

		// 生成验证码文本
		String capText = captchaProducer.createText();
		session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
		System.out.println("生成验证码文本====" + capText);
		// 利用生成的字符串构建图片
		BufferedImage bi = captchaProducer.createImage(capText);
		ServletOutputStream out = response.getOutputStream();
		ImageIO.write(bi, "jpg", out);

		try {
			out.flush();
		} finally {
			out.close();
		}
	}

	@RequestMapping("jsp/createMenu")
	@ResponseBody
	public ResultVo createMenu(HttpSession session, Model model) {
		return ResultVo.success(roleService.getPermissionByRoleId(1));
	}

	@RequestMapping("jsp/logout")
	public String logout(HttpServletRequest request) {
		// 清除session
		request.getSession().removeAttribute(Constants.USER_SESSION);
		return "redirect:/login/index";
	}

	@RequestMapping("login")
	public String logindo(String userCode, String userPassword, HttpServletRequest request, Model model) {
		User user = userService.getUser(userCode);
		if (user == null) {
			// 用户不存在
			model.addAttribute("error", "用户不存在");
			// return "login";
		} else if (!user.getUserPassword().equals(userPassword)) {
			// 密码错误
			model.addAttribute("error", "密码错误");
		} else {
			//
			request.getSession().setAttribute(Constants.USER_SESSION, user);
			return "redirect:/jsp/frame/index";
		}
		return "login";
	}

	@RequestMapping("sendSms")
	@ResponseBody
	public ResultVo sendSms() {
		DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FyjrkLKsag3d6fpsUsv",
				"kgEAuwPdkNnYdkP7WyiTd6wijPQIXu");
		IAcsClient client = new DefaultAcsClient(profile);

		CommonRequest request = new CommonRequest();
		request.setSysMethod(MethodType.POST);
		request.setSysDomain("dysmsapi.aliyuncs.com");
		request.setSysVersion("2020-12-19");
		request.setSysAction("SendSms");
		request.putQueryParameter("RegionId", "cn-hangzhou");
		try {
			CommonResponse response = client.getCommonResponse(request);
			System.out.println(response.getData());
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
		return ResultVo.success("短信发送成功");
	}

	@Autowired
	private JavaMailSender mailSender;

	@RequestMapping("sendMail")
	@ResponseBody
	public ResultVo sendMail() {
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom("ezerg@126.com");
		message.setTo("ezerg@qq.com");
		message.setSubject("主题：简单邮件");
		message.setText("测试邮件内容");
		//
		mailSender.send(message);
		return ResultVo.success("发送邮件成功");
	}

}
